<template>
     <div style="display:flex;flex-direction:column;height:100%">
        <div style="display:flex;">
            <Card bordered dis-hover class="model-list-css">
                <p slot="title">作业模板</p>
                <a href="#" slot="extra" @click.prevent="addModel">
                    <Icon type="md-add" />
                </a>
                <div style="width:300px;overflow-y:auto;overflow-x:hidden" ref="divCard">
                    <div v-for="(item,index) in formworks" style="width:300px;padding:15px;border:1px solid #e8eaec">
                        <div style="display:flex;width:85%;justify-content:space-between"> 
                            <p style="font-size:16px;">{{ item.modelName }}</p>
                            <div>
                                <Button type="text" icon="ios-create-outline"  v-if="item.createUserId == createUserId" @click="updateModel(item)"></Button>
                                <Button type="text" icon="md-close " v-if="item.createUserId == createUserId" @click="deleteModel(item)"></Button>
                            </div>
                        </div>
                        <div class="query-form">
                            <Button size="large"  class="ivu-btn-delete" @click="workAreaDispaly(index)">作业项配置</Button>
                            <Button size="large"  class="ivu-btn-delete" @click="checkUpAreaDisplay(index)">检查项配置</Button>
                          <!--  <Button size="large"  class="ivu-btn-delete" @click="baseItemAreaDisplay(index)">基础信息配置</Button>  -->
                            <Button size="large"  class="ivu-btn-delete" @click="assistDisplay(index)">辅助项配置</Button>
                        </div>
                    </div>
                </div>
            </Card>
            <Card  bordered dis-hover style="margin-left:5px;flex-grow:1;height:100%;overflow-y:auto;">
                <Row v-show="workDispaly" class="query-form">
                    <Col span="24">
                        <Button size="large"  class="ivu-btn-add" icon="md-add" v-if="!viewFlag" @click="addModelSub">新建</Button>
                    <!--    <Button size="large"  class="ivu-btn-add" icon="md-add" v-if="!viewFlag" @click="addModelSubChild">新建子项</Button> -->
                        <Button size="large"  class="ivu-btn-delete" icon="md-trash"  v-if="!viewFlag" @click="deleteModelSubs">批量删除</Button>
                        <Button size="large"  class="ivu-btn-delete"  v-if="!viewFlag" @click="sortModelSub">排序</Button>
                    </Col>
                </Row>
                <div v-show="workDispaly">
                    <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="columns"
                        :data="modelSubs" width="100%" :height="tableHeight" highlight-row stripe size="small"
                            @on-select="onSelect" @on-select-cancel="onSelectAll" @on-select-all="onSelectAll" @on-select-all-cancel="onSelectAllCancel"></Table>
                    <Page :total="pagedata.total" :current="pagedata.currentPage" :page-size="pagedata.pageSize"
                        @on-page-size-change="pageSizeChange" @on-change="pageChange" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
                </div>

                <div v-show="checkUpDisplay">
                    <Card v-for="(item,index) in workItems" :key="item.modelSubId" style="margin-bottom:10px">
                        <p slot="title">
                        {{item.parentModelSubName!=null?item.parentModelSubName+"-":""}}{{item.modelSubName}}：{{item.modelSubDesc}}
                        </p>
                        <div slot="extra" >
                            <Button size="large"  class="check-up-button" icon="md-add"  v-if="!viewFlag" @click="addWorkItems(index,item.modelSubId)">添加</Button>
                            <Button size="large"  class="check-up-button" icon="md-trash"  v-if="!viewFlag" @click="deleteWorkItems(index)">批量删除</Button>
                            <Button size="large"  class="check-up-button" v-if="!viewFlag" @click="sortWorkItems(index)">排序</Button>
                        </div>
                        <Row>
                            <Col>
                                <CheckboxGroup v-model="checkDatas[index]">
                                    <Row :class="rowIndex%2==0?detailRow1:detailRow2" v-for="(row,rowIndex) in item.rows" :key="row.itemId">
                                        <Col span="6" v-for="child in row" :key="child.itemId"  style="padding-left: 10px;border-right: 1px solid #E0E0EC;" >
                                            <Checkbox  :label="child.itemId"  v-if="child.itemId!=undefined">
                                                <span>{{child.itemName}}</span>
                                            </Checkbox>
                                            <Button v-if="child.itemId!=undefined" type="text" icon="ios-create-outline " @click="updateWorkItem(child,index)" style="margin-top: 10px;margin-right: 30px;float: right" ></Button>
                                            <Button v-else type="text"></Button>
                                        </Col>
                                    </Row>
                                </CheckboxGroup>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' @click="submitModelSubs" >保存</Button>
                                    <Button type="primary"  class='ivu-btn-reset' @click="resetModelSubs" >重置</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleModelSubs" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </card>
                </div>

                <Row v-show="baseItemDisplay" class="query-form"  >
                    <Col span="24">
                        <Button size="large"  class="ivu-btn-add" icon="md-add"  v-if="!viewFlag" @click="addBaseItem">新建</Button>
                        <Button size="large"  class="ivu-btn-delete" icon="md-trash"  v-if="!viewFlag" @click="deleteBaseItems">批量删除</Button>
                        <Button size="large"  class="ivu-btn-delete"  v-if="!viewFlag" @click="sortBaseItem">排序</Button>
                    </Col>
                </Row>
                <div v-show="baseItemDisplay">
                    <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="baseItemColumns"
                        :data="baseItems" width="100%" :height="tableHeight" highlight-row stripe size="small"
                            @on-select="onSelect" @on-select-cancel="onSelectAll" @on-select-all="onSelectAll" @on-select-all-cancel="onSelectAllCancel"></Table>
                    <Page :total="pagedata.total2" :current="pagedata.currentPage2" :page-size="pagedata.pageSize2"
                        @on-page-size-change="pageSizeChange2" @on-change="pageChange2" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
                </div>
            </Card>
        </div>

        <Modal  :title="title1" :transfer=false footer-hide v-model="modelAdd"  :width=600 >
            <Row>
                <Col span="24">
                    <Form ref="formCustom" :model="formCustom" :rules="ruleValidate"  label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="组织机构" prop="orgName">
                                    <cascaderDom ref="orgName"  @setOrgInfo="setOrgId"  @setOrgname="setOrgName" :currentOrgId = "formCustom.belongOrgId" 
                                    :currentOrgName = "formCustom.belongOrgName"  :rootAffordIf="true" :orgTree = "true"  :rootOrgId="rootOrgId"></cascaderDom> 
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="模版名称" prop="modelName">
                                    <Input  v-model="formCustom.modelName" type="text"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="模版类型" prop="modelType">
                                    <Select class="query-form-el" filterable v-model="formCustom.modelType"  >
                                        <Option v-for="item in modelTypeList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="应用到下属单位" prop="ifApplyUnit">
                                    <Select v-model="formCustom.ifApplyUnit">
                                        <Option value="1">是</Option>
                                        <Option value="0">否</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="模板说明" prop="modelDesc">
                                    <Input type="textarea" v-model="formCustom.modelDesc"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' @click="submitModel" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleModel" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
        </Modal>

        <Modal  :title="title2" :transfer=false footer-hide v-model="modelSubAdd"  :width=600 >
            <Row>
                <Col span="24">
                    <Form ref="modelSubCustom" :model="modelSubCustom" :rules="modelSubRuleValidate"  label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="作业项名称" prop="modelSubName">
                                    <Input  v-model="modelSubCustom.modelSubName" type="text"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="作业项描述" prop="modelSubDesc">
                                    <Input  v-model="modelSubCustom.modelSubDesc" type="text"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' @click="submitModelSub" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleModelSub" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
        </Modal>

        <Modal  :title="title3" :transfer=false footer-hide v-model="modelSubChildAdd"  :width=600 >
            <Row>
                <Col span="24">
                    <Form ref="modelSubChildCustom" :model="modelSubChildCustom" :rules="modelSubChildRuleValidate"  label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="上级作业项" prop="parentId">
                                    <Select  v-model="modelSubChildCustom.parentId"></Input>
                                        <Option v-for="item in modelSubLists" :key="item.modelSubId" :value="item.modelSubId">{{item.modelSubName}}</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="作业项名称" prop="modelSubName">
                                    <Input  v-model="modelSubChildCustom.modelSubName" type="text"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="作业项描述" prop="modelSubDesc">
                                    <Input type="textarea" v-model="modelSubChildCustom.modelSubDesc"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' @click="submitModelSubChild" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleModelSubChildren" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
        </Modal>

        <Modal  :title="title4" :transfer=false footer-hide v-model="baseItemAddOrUpdate"  :width=600 >
            <Row>
                <Col span="24">
                    <Form ref="baseItemCustom" :model="baseItemCustom" :rules="baseItemRuleValidate"  label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="基础项名称" prop="itemName">
                                    <Input  v-model="baseItemCustom.itemName" type="text"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="基础项类型" prop="dataType">
                                    <RadioGroup  v-model="baseItemCustom.dataType">
                                        <Radio label="1">选择</Radio>
                                        <Radio label="2">录入</Radio>
                                    </RadioGroup>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' @click="submitBaseItem" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleBaseItem" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
        </Modal>

        <Modal  :title="title5" :transfer=false footer-hide v-model="workItemAddOrUpdate"  :width=600 >
            <Row>
                <Col span="24">
                    <Form ref="workItemCustom" :model="workItemCustom" :rules="workItemRuleValidate"  label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="检查项名称" prop="itemName">
                                    <Input  v-model="workItemCustom.itemName" type="text"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="检查项类型" prop="dataType">
                                    <RadioGroup  v-model="workItemCustom.dataType">
                                        <Radio label="1">勾选项</Radio>
                                        <Radio label="2">输入项</Radio>
                                        <Radio label="3">必填项</Radio>
                                    </RadioGroup>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' @click="submitWorkItem" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleWorkItem" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
        </Modal>

        <Modal  title="排序" :transfer=false footer-hide v-model="sortBaseItemView"  :width=800 >
            <Row>
                <Col span="24">
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23" >
                            <Table ref='table1' style="margin-top: 10px" :loading="loading" border :columns="baseItemSortColumn"
                            :data="baseItemSortDatas" width="100%" highlight-row stripe size="small"></Table>
                        </Col>
                    </Row>
                    <Row type="flex" justify="center" class="code-row-bg">
                        <Col span="23">
                            <Button type="primary"  class='ivu-btn-save' @click="submitBaseItemSort" >确定</Button>
                            <Button type="primary"  class='ivu-btn-cancel' @click="cancleBaseItemSort" >取消</Button>
                        </Col>
                    </Row>
                </Col>
            </Row>
        </Modal>

        <Modal  title="排序" :transfer=false footer-hide v-model="sortModelSubView"  :width=800 >
            <Row>
                <Col span="24">
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23" >
                            <Table ref='table1' style="margin-top: 10px" :loading="loading" border :columns="modelSubSortColumn"
                            :data="modelSubSortDatas" width="100%" highlight-row stripe size="small"></Table>
                        </Col>
                    </Row>
                    <Row type="flex" justify="center" class="code-row-bg">
                        <Col span="23">
                            <Button type="primary"  class='ivu-btn-save' @click="submitModelSubSort" >确定</Button>
                            <Button type="primary"  class='ivu-btn-cancel' @click="cancleModelSubSort" >取消</Button>
                        </Col>
                    </Row>
                </Col>
            </Row>
        </Modal>

        <Modal  title="排序" :transfer=false footer-hide v-model="sortWorkItemView"  :width=800 >
            <Row>
                <Col span="24">
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23" >
                                <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="workItemSortColumn"
                                :data="workItemSortDatas" width="100%"  highlight-row stripe size="small"></Table>
                        </Col>
                    </Row>
                    <Row type="flex" justify="center" class="code-row-bg">
                        <Col span="23">
                            <Button type="primary"  class='ivu-btn-save' @click="submitWorkItemSort" >确定</Button>
                            <Button type="primary"  class='ivu-btn-cancel' @click="cancleWorkItemSort" >取消</Button>
                        </Col>
                    </Row>
                </Col>
            </Row>
        </Modal>

        <Modal  title="辅助项信息" :transfer=false footer-hide v-model="assistView"  :width=600 >
            <Row>
                <Col span="24">
                    <Form ref="formCustom" :model="formCustom" :rules="ruleValidate"  label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23" >
                                <FormItem label="辅助信息" prop="orgName">
                                   <Input v-model="formCustom.notes" type="textarea"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23">
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' v-if="!viewFlag" @click="submitModel" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="cancleModel" >取消</Button>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>


<script>
	import {Api} from '../../../../api/index.js'
    import { sessionStorage, cookie } from '../../../../common/cacheUtils.js';
    var cascaderDom = () => import('@/components/cascader-dom.vue');
	export default {
        components:{
            cascaderDom
        },
        data(){
            return {
                title1:"",
                title2:"",
                title3:"",
                title4:"",
                title5:"",
                viewFlag:true,
                detailRow1: 'detailRow1',
                detailRow2:'detailRow2',
                selectedModelSubInex:0,
                applyUnit:false,
                loading:false,
                workDispaly:true,
                checkUpDisplay:false,
                baseItemDisplay:false,
                modelTypeList:[],
                formworks: [],
                modelSubs: [],
                modelSubLists: [],
                workItems: [],
                baseItems: [],
                selected: [],
                checkDatas: [],
                workItemSortDatas:[],
                modelAdd:false,
                modelSubAdd:false,
                modelSubChildAdd:false,
                assistView: false,
                sortModelSubView:false,
                sortWorkItemView:false,
                sortBaseItemView: false,
                workItemAddOrUpdate: false,
                baseItemAddOrUpdate: false,
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                    total2: 0,
                    pageSize2: 10,
                    currentPage2: 1
                },
                pageSizeArray: [5, 10, 20, 50],
                tableHeight:200,
                modelSubSortDatas:[],
                baseItemSortDatas:[],
                modelSubChilds: [],
                workDetails:[],
                rootOrgId:'',
                columns:[
                    {
                        type: 'selection',
                        width: 80,
                        align: 'center',
                        fixed: 'left',
                    },
                    {
                        title: '作业项名称',
                        align: 'center',
                        key: 'modelSubName',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.modelSubName==null?"":params.row.modelSubName)
                            ]);
                        }
                    },
                    {
                        title: '上级作业项',
                        align: 'center',
                        key: 'parentModelSubName',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.parentModelSubName==null?"":params.row.parentModelSubName)
                            ]);
                        }
                    },
                    {
                        title: '描述',
                        align: 'center',
                        key: 'modelSubDesc',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.modelSubDesc==null?"":params.row.modelSubDesc)
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        fixed: 'right',
                        width: 150,
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.updateModelSub(params.row);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        color: 'red'
                                    },
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '数据删除',
                                                content: '<p>确定删除该条信息</p>',
                                                okText: '确认',
                                                onOk: () => {
                                                    this.deleteModelSub(params.row.modelSubId);
                                                },
                                                cancelText: '取消'
                                            });            
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                baseItemColumns:[
                    {
                        type: 'selection',
                        width: 80,
                        align: 'center',
                        fixed: 'left',
                    },
                    {
                        title: '项目',
                        align: 'center',
                        key: 'itemName'
                    },
                    {
                        title: '录入类型',
                        align: 'center',
                        key: 'dataType',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.dataType=='1'?"选择":"录入")
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        fixed: 'right',
                        width: 150,
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.updateBaseItem(params.row);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        color: 'red'
                                    },
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '数据删除',
                                                content: '<p>确定删除该条信息</p>',
                                                okText: '确认',
                                                onOk: () => {
                                                    this.deleteBaseItem(params.row.itemId);
                                                },
                                                cancelText: '取消'
                                            });
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                modelSubSortColumn:[
                    {
                        title: '作业项名称',
                        align: 'center',
                        key: 'modelSubName',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.modelSubName==null?"":params.row.modelSubName)
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        key: 'view',
                        fixed: 'right',
                        width: 150,
                        render: (h, params) => {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'md-arrow-round-up'
                                    },
                                    style: {
                                        fontSize: '18px',
                                        color: '#559DF9'
                                    },
                                    on: {
                                        click: () => {
                                            this.upSort(params.index)
                                        }
                                    }
                                }),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('Icon', {
                                    props: {
                                        type: 'md-arrow-round-down'
                                    },
                                    style: {
                                        fontSize: '18px',
                                        color: '#559DF9'
                                    },
                                    on: {
                                        click: () => {
                                            this.downSort(params.index);
                                        }
                                    }
                                })
                            ]);
                        }
                    }
                ],
                workItemSortColumn:[
                    {
                        title: '检查项名称',
                        align: 'center',
                        key: 'itemName',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.itemName==null?"":params.row.itemName)
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        key: 'view',
                        fixed: 'right',
                        width: 150,
                        render: (h, params) => {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'md-arrow-round-up'
                                    },
                                    style: {
                                        fontSize: '18px',
                                        color: '#559DF9'
                                    },
                                    on: {
                                        click: () => {
                                            this.upSortWorkItem(params.index)
                                        }
                                    }
                                }),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('Icon', {
                                    props: {
                                        type: 'md-arrow-round-down'
                                    },
                                    style: {
                                        fontSize: '18px',
                                        color: '#559DF9'
                                    },
                                    on: {
                                        click: () => {
                                            this.downSortWorkItem(params.index);
                                        }
                                    }
                                })
                            ]);
                        }
                    }
                ],
                baseItemSortColumn:[
                    {
                        title: '基础项名称',
                        align: 'center',
                        key: 'itemName',
                        render:(h,params)=>{
                            return h('div',[
                                (params.row.itemName==null?"":params.row.itemName)
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        key: 'view',
                        fixed: 'right',
                        width: 150,
                        render: (h, params) => {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'md-arrow-round-up'
                                    },
                                    style: {
                                        fontSize: '18px',
                                        color: '#559DF9'
                                    },
                                    on: {
                                        click: () => {
                                            this.upSortBaseItem(params.index)
                                        }
                                    }
                                }),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('Icon', {
                                    props: {
                                        type: 'md-arrow-round-down'
                                    },
                                    style: {
                                        fontSize: '18px',
                                        color: '#559DF9'
                                    },
                                    on: {
                                        click: () => {
                                            this.downSortBaseItem(params.index);
                                        }
                                    }
                                })
                            ]);
                        }
                    }
                ],
                formCustom:{},
                modelSubCustom:{},
                workItemCustom:{},
                baseItemCustom:{},
                modelSubIndex:0,
                workItemIndex:0,
                modelSubChildCustom:{},
                sortCustom:{},
                orgId:'',
                orgName:'',
                currentModelId:'',
                createUserId: '',
                sortRuleValidate:{

                },
                baseItemRuleValidate:{

                },
                modelSubRuleValidate:{

                },
                modelSubChildRuleValidate:{

                },
                workItemRuleValidate:{

                },
                ruleValidate: {
                    generaName: [
                        { required: true, message: "", trigger: "change" }
                        ],
                        generaCode: [
                        { required: true, message: "", trigger: "change" }
                        ], 
                        belongBusiness: [
                        { required: true, message: "", trigger: "change" }
                        ]
                }
            }
        },
        watch:{
            oOrgName(val){
                setTimeout(()=>{
                    this.$refs.orgId.setCurrentOrg();
                },100);
            },
        },
        mounted:function(){
            this.rootOrgId = sessionStorage('userId').getObject('userId').orgInfo.parentId;
            this.createUserId = sessionStorage('userId').getObject('userId').userId;
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 245;
       //     this.$refs.divCard.style.height = (window.innerHeight - 180 ) + 'px';
            this.query();
            this.selectCodingSort();
        },
        methods:{
            selectCodingSort() {
                this.$http.get(Api.queryCodeDetailBySortId + '/ptw-100001', {params: {}}).then((res) => {
                    this.modelTypeList = res.data.data;
                })
            },
            query(){
                let data = {
                    belongOrgId: sessionStorage('userId').getObject('userId').orgInfo.orgId,
                    userId:  sessionStorage('userId').getObject('userId').userId
                }
                this.$http.get(Api.queryPtwModelList, {params: data}).then((res) => {
                    this.formworks = res.data;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            queryModelSubs(){
                let data = {
                    modelId:  this.currentModelId,
                    currentPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize
                }
                this.$http.get(Api.getPtwModelSubPageList, {params: data}).then((res) => {
                    this.modelSubs = res.data.data;
                    this.pagedata.total = res.data.totalSize;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            queryPtwModelNoParentSub(){
                this.$http.get(Api.getPtwModelNoParentSub+"/"+this.currentModelId).then((res) => {
                    this.modelSubLists = res.data;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            queryBaseItems(){
                let data = {
                    currentPage: this.pagedata.currentPage2,
                    size: this.pagedata.pageSize2,
                    modelId: this.currentModelId
                }
                this.$http.get(Api.getPtwModelBaseItemPageList,{params: data}).then((res) => {
                    this.baseItems = res.data.data;
                    this.pagedata.total2 = res.data.totalSize;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            getPtwModelItemByModelSubId(modelSubIndex){
                 this.$http.get(Api.getPtwModelItemByModelSubId+"/"+this.workItems[modelSubIndex].modelSubId).then((res) => {
                    let childs = res.data;
                    this.workItems[modelSubIndex].itemList = res.data;
                    let rowNum = childs.length / 4;
                    var rows = [];
//                    this.workItems[modelSubIndex].rows.splice(0,this.workItems[modelSubIndex].rows.length);
                    for (let i=0; i < rowNum; i++) {
                        rows[i] = [];
                        for(let j = 0; j < childs.length - i * 4; j ++){
                            let flag = (j + 1) % 4;
                            let index = i * 4 + j;
                            this.$set(rows[i],j,childs[index]);
                            if(flag == 0 ){
                                break;
                            }
                        }
                    }
                    //如果最后一行不满4个，添加空数据补满
                    if(rows.length>0){
                        let diff = 4 - rows[rows.length-1].length;
                        for(let i = 0; i < diff;i++){
                            rows[rows.length-1].push({});
                        }
                    }
                    this.$set(this.workItems[modelSubIndex],'rows',rows);
                    this.$forceUpdate();
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            onSelect(selection,row){
                this.selected = selection;
            },
            onSelectCancle(selection,row){
                this.selected = selection;
            },
            onSelectAll(selection){
                this.selected = selection;
            },
            onSelectAllCancel(selection,row){
                this.selected = selection;
            },
            pageSizeChange(pageSize){
                this.pagedata.pageSize = pageSize;
                this.queryModelSubs();
            },
            pageChange(pageNum){
                this.pagedata.currentPage = pageNum;
                this.queryModelSubs();
            },
            pageSizeChange2(pageSize){
                this.pagedata.pageSize2 = pageSize;
                this.queryBaseItems();
            },
            pageChange2(pageNum){
                this.pagedata.currentPage2 = pageNum;
                this.queryBaseItems();
            },
            workAreaDispaly(index){
                this.viewFlag = (this.formworks[index].createUserId == this.createUserId?false:true);
                this.currentModelId = this.formworks[index].modelId;
                this.queryModelSubs();
                this.selected = [];
                this.checkUpDisplay = false;
                this.baseItemDisplay = false;
                this.workDispaly = true;
            },
            checkUpAreaDisplay(index){
                this.viewFlag = (this.formworks[index].createUserId == this.createUserId?false:true);
                this.currentModelId = this.formworks[index].modelId;
                this.selected = [];
                this.workDispaly = false;
                this.baseItemDisplay = false;
                this.checkUpDisplay = true;
                this.getPtwModelItemByModelId();
                this.modelSubChilds = [];
            },
            baseItemAreaDisplay(index){
                this.viewFlag = (this.formworks[index].createUserId == this.createUserId?false:true);
                this.currentModelId = this.formworks[index].modelId;
                this.selected = [];
                this.queryBaseItems();
                this.workDispaly = false;
                this.checkUpDisplay = false; 
                this.baseItemDisplay = true;   
            },
            assistDisplay(index){
                this.viewFlag = (this.formworks[index].createUserId == this.createUserId?false:true);
                this.formCustom = JSON.parse(JSON.stringify(this.formworks[index]));
                this.assistView = true;
            },
            addModel(){
                this.formCustom ={
                    belongOrgId:'',
                    belongOrgName:'',
                    modelName:'',
                    ifApplyUnit:'',
                    modelDesc:'',
                    createUserId: this.createUserId
                }
                this.title1 = "作业模板新建";
                this.modelAdd = true;
            },
            updateModel(item){
                this.formCustom = JSON.parse(JSON.stringify(item));
            //    this.formCustom.belongOrgId = item.belongOrgId;
            //    this.formCustom.belongOrgName = item.belongOrgName;
                setTimeout(()=>{
                    this.$refs.orgName.setCurrentOrg();
                },200)
                this.title1 = "作业模板修改"
                this.modelAdd = true;
            },
            deleteModel(item){
                this.$Modal.confirm({
                    title: '数据删除',
                    content: '<p>确定删除该条信息</p>',
                    okText: '确认',
                    onOk: () => {
                        this.$http.get(Api.deletePtwModel + "?modelId=" +item.modelId).then((res) => {
                            if (res.data.result == "success") {
                                this.query();
                                this.$Message.info('删除成功');
                            } else {
                                this.$Message.info(res.data.message);
                            }
                        });
                    },
                    cancelText: '取消'
                });   
            },
            addModelSub(){
                this.modelSubCustom = {
                    modelSubDesc : "",
                    modelId:this.currentModelId,
                    modelSubName : "",
                    seq:this.modelSubs.length,
                    createUserId: this.createUserId
                }
                this.title2 = "新建作业项"
                this.modelSubAdd = true;
            },
            addModelSubChild(){
                this.queryPtwModelNoParentSub();  
                this.modelSubChildCustom = {
                    modelSubId:"",
                    modelSubName:"",
                    modelSubDesc:"",
                    parentId:"",
                    modelId:this.currentModelId,
                    createUserId: this.createUserId
                }
                this.title3 = "作业子项新建";
                this.modelSubChildAdd = true;
            },
            addWorkItems(index,parentId){
                this.workItemCustom = {
                    modelSubId:parentId,
                    itemName:"",
                    dataType:"1",
                    parentId:parentId,                   
                    createUserId: this.createUserId,
                    seq:this.workItems[index].itemList.length
                },
                this.selectedModelSubInex = index;
                this.title5 = "检查项添加";
                this.workItemAddOrUpdate = true;
            },
            addBaseItem(){
                this.baseItemCustom = {
                    modelId: this.currentModelId,
                    itemName: '',
                    dataType: '1',
                    seq:this.baseItems.length,
                    createUserId: this.createUserId
                }
                this.title3 = "基础项新建";
                this.baseItemAddOrUpdate = true;
            },
            deleteWorkItems(index){
                let ids = '';
                this.checkDatas[index].forEach((item)=>{
                    ids = ids + item + ';';
                })
                ids = ids.substr(0, ids.length - 1);
                this.$http.get(Api.deletePtwModelItem + "?itemId=" +ids).then((res) => {
                    if (res.data.result == "success") {
                        this.getPtwModelItemByModelSubId(index);
                        this.$Message.info('删除成功');
                    } else {
                        this.$Message.info(res.data.message);
                    }
                });
            },
            updateWorkItem(row,index){
                this.selectedModelSubInex = index;
                this.workItemCustom = JSON.parse(JSON.stringify(row));
                this.title5 = "检查项修改";
                this.workItemAddOrUpdate = true;
            },
            sortModelSub(){
                this.modelSubSortDatas = JSON.parse(JSON.stringify(this.modelSubs)); 
                this.sortModelSubView = true;
            },
            sortWorkItems(index){
                this.selectedModelSubInex = index;
                this.workItemSortDatas = JSON.parse(JSON.stringify(this.workItems[index].itemList)); 
                this.sortWorkItemView = true;
            },
            sortBaseItem(){
                this.baseItemSortDatas = JSON.parse(JSON.stringify(this.baseItems)); 
                this.sortBaseItemView = true;
            },
            upSort(index){
                this.modelSubSortDatas[index-1].seq = index;
                this.modelSubSortDatas[index].seq = index-1;
                var row = this.modelSubSortDatas[index-1];
                this.$set(this.modelSubSortDatas,index-1,this.modelSubSortDatas[index]);
                this.$set(this.modelSubSortDatas,index,row);
            },
            downSort(index){
                this.modelSubSortDatas[index+1].seq = index;
                this.modelSubSortDatas[index].seq = index+1;
                var row = this.modelSubSortDatas[index+1];
                this.$set(this.modelSubSortDatas,index+1,this.modelSubSortDatas[index]);
                this.$set(this.modelSubSortDatas,index,row);
            },
            upSortWorkItem(index){
                this.workItemSortDatas[index-1].seq = index;
                this.workItemSortDatas[index].seq = index-1;
                var row = this.workItemSortDatas[index-1];
                this.$set(this.workItemSortDatas,index-1,this.workItemSortDatas[index]);
                this.$set(this.workItemSortDatas,index,row);
            },
            downSortWorkItem(index){
                this.workItemSortDatas[index+1].seq = index;
                this.workItemSortDatas[index].seq = index+1;
                var row = this.workItemSortDatas[index+1];
                this.$set(this.workItemSortDatas,index+1,this.workItemSortDatas[index]);
                this.$set(this.workItemSortDatas,index,row);
            },
            upSortBaseItem(index){
                this.baseItemSortDatas[index-1].seq = index;
                this.baseItemSortDatas[index].seq = index-1;
                var row = this.baseItemSortDatas[index-1];
                this.$set(this.baseItemSortDatas,index-1,this.baseItemSortDatas[index]);
                this.$set(this.baseItemSortDatas,index,row);
            },
            downSortBaseItem(index){
                this.baseItemSortDatas[index+1].seq = index;
                this.baseItemSortDatas[index].seq = index+1;
                var row = this.baseItemSortDatas[index+1];
                this.$set(this.baseItemSortDatas,index+1,this.baseItemSortDatas[index]);
                this.$set(this.baseItemSortDatas,index,row);
            },
            deleteModelSub(id){
                this.$http.get(Api.deletePtwModelSub + "?modelSubId=" +id).then((res) => {
                    if (res.data.result == "success") {
                        this.queryModelSubs();
                        this.$Message.info('删除成功');
                    } else {
                        this.$Message.info(res.data.message);
                    }
                });
            },
            deleteBaseItem(id){
                this.$http.get(Api.deletePtwModelBaseItem + "?itemId=" +id).then((res) => {
                    if (res.data.result == "success") {
                        this.queryBaseItems();
                        this.$Message.info('删除成功');
                    } else {
                        this.$Message.info(res.data.message);
                    }
                });
            },
            deleteModelSubs(){
                this.$Modal.confirm({
                    title: '数据删除',
                    content: '<p>确定删除该条信息</p>',
                    okText: '确认',
                    onOk: () => {
                       var modelSubIds = "";
                        this.selected.forEach(item=>{
                            if(modelSubIds == "")
                                modelSubIds = item.modelSubId;
                            else
                                modelSubIds += (";" + item.modelSubId);
                        })
                        this.deleteModelSub(modelSubIds);
                    },
                    cancelText: '取消'
                }); 
            },
            deleteBaseItems(){
                var baseItemIds = "";
                this.selected.forEach(item=>{
                    if(baseItemIds == "")
                        baseItemIds = item.itemId;
                    else
                        baseItemIds += (";" + item.itemId);
                })
                this.deleteBaseItem(baseItemIds);
            },
            setOrgId(data){
                this.formCustom.belongOrgId = data[data.length - 1];
            },
            setOrgName(data){
                this.formCustom.belongOrgName = data;
            },
            updateModelSub(row){
                this.title2 = "作业子项修改";
                if(row.parentModelSubId == null){
                    this.modelSubCustom = JSON.parse(JSON.stringify(row)); 
                    this.modelSubAdd = true;
                }else{
                    this.queryPtwModelNoParentSub();
                    this.modelSubChildCustom = JSON.parse(JSON.stringify(row)); 
                    this.modelSubChildCustom.parentId = row.parentModelSubId;
                    this.modelSubChildAdd = true;
                }
            },
            updateBaseItem(row){
                this.baseItemCustom =  JSON.parse(JSON.stringify(row));
                this.title3 = "基础项修改"
                this.baseItemAddOrUpdate = true;
            },
            submitModel(){
                this.$http.post(Api.saveOrUpdatePtwModel, this.formCustom).then((res) => {
                    if (res.data.result == "success") {
                        this.query();
                        if(this.modelAdd){
                            this.modelAdd = false;
                            this.$Message.info('添加成功');
                        }
                        if(this.assistView){
                            this.assistView = false;
                            this.$Message.info('修改成功');
                        }
                    } else {
                        this.$Message.info(res.data.message);
                    }
                });
            },
            cancleModel(){
                this.modelAdd = false;
                this.assistView = false;
            },
            submitModelSub(){
                this.$http.post(Api.saveOrUpdatePtwModelSub, this.modelSubCustom).then((res) => {
                    if (res.data.result == "success") {
                        this.queryModelSubs();
                        this.modelSubAdd = false;
                        this.$Message.info('添加成功');
                    } else {
                        this.$Message.info(res.data.message);
                    }
                });
            },
            cancleModelSub(){
                this.modelSubAdd = false;
            },
            submitModelSubChild(){
                this.$http.post(Api.saveOrUpdatePtwModelSub, this.modelSubChildCustom).then((res) => {
                    if (res.data.result == "success") {
                        this.queryModelSubs();
                        this.modelSubChildAdd = false;
                        this.$Message.info('添加成功');
                    } else {
                        this.$Message.info(res.data.message);
                    }
                });
            },
            submitModelSubs(){

            },
            cancleModelSubs(){

            },
            resetModelSubs(){

            },
            cancleModelSubChildren(){
                this.modelSubChildAdd = false;
            },
            cancleWorkItem(){
                this.workItemAddOrUpdate = false;  
            },
            submitModelSubSort(){
                var index1 = 1;
                var max = this.modelSubSortDatas.length;
                this.modelSubSortDatas.forEach(item=>{
                    this.$http.post(Api.saveOrUpdatePtwModelSub, item).then((res) => {
                        if(index1 == max){
                            this.sortModelSubView = false;
                            if(res.data.result == "success"){
                                this.queryModelSubs();
                            }
                        }
                        index1++;
                    });
                })
            },
            submitBaseItemSort(){
                var index1 = 1;
                var max = this.baseItemSortDatas.length;
                this.baseItemSortDatas.forEach(item=>{
                    this.$http.post(Api.saveOrUpdatePtwModelBaseItem, item).then((res) => {
                        if(index1 == max){
                            this.sortBaseItemView = false;
                            if(res.data.result == "success"){
                                this.queryBaseItems();
                            }
                        }
                        index1++;
                    });
                })
            },
            cancleBaseItemSort(){
                this.sortBaseItemView = false;
            },
            submitWorkItem(){
                this.$http.post(Api.saveOrUpdatePtwModelItem , this.workItemCustom).then((res) => {
                    this.$Message.success("保存成功")
                    this.workItemAddOrUpdate = false;
                    this.getPtwModelItemByModelSubId(this.selectedModelSubInex);
                }).catch(e => {
                    this.workItemAddOrUpdate = false;
                    this.getPtwModelItemByModelSubId(this.selectedModelSubInex);
                    this.$Message.error("数据保存失败")
                })
            },
            submitBaseItem(){
                this.$http.post(Api.saveOrUpdatePtwModelBaseItem , this.baseItemCustom).then((res) => {
                    this.$Message.success("保存成功")
                    this.baseItemAddOrUpdate = false;
                    this.queryBaseItems();
                }).catch(e => {
                    this.workItemAddOrUpdate = false;
                    this.queryBaseItems();
                    this.$Message.error("数据保存失败")
                })
            },
            cancleBaseItem(){
                this.baseItemAddOrUpdate = false;
            },
            submitWorkItemSort(){
                var index1 = 1;
                var max = this.workItemSortDatas.length;
                this.workItemSortDatas.forEach(item=>{
                    this.$http.post(Api.saveOrUpdatePtwModelItem, item).then((res) => {
                        if(index1 == max){
                            this.sortWorkItemView = false;
                            if(res.data.result == "success"){
                                this.getPtwModelItemByModelSubId(this.selectedModelSubInex); 
                            }
                        }
                        index1++;
                    });
                })
            },
            cancleWorkItemSort(){
                this.sortWorkItemView = false;
            },
            cancleModelSubSort(){
                this.sortModelSubView = false;
            },
            getPtwModelItemByModelId(){
                this.$http.get(Api.getPtwModelItemByModelId+"/"+this.currentModelId).then((res) => {
                    this.workItems = res.data;
                    this.workItems.map(item =>{
                        let childs = item.itemList;
                        let rowNum = childs.length / 4;
                        item.rows = [];
                        for (let i=0; i < rowNum; i++) {
                            item.rows[i] = [];
                            for(let j = 0; j < item.itemList.length - i * 4; j ++){
                                let flag = (j + 1) % 4;
                                let index = i * 4 + j;
                                item.rows[i].push(item.itemList[index]);
                                if(flag == 0 ){
                                    break;
                                }
                            }
                        }
                        //如果最后一行不满4个，添加空数据补满
                        if(item.rows.length>0){
                            let diff = 4 - item.rows[item.rows.length-1].length;
                            for(let i = 0; i < diff;i++){
                                item.rows[item.rows.length-1].push({});
                            }
                        }
                    })
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            }
        }
            
    }

</script>

<style lang='css' scoped>
    @import '../../../../index.css';

    .model-list-css{

    }

    .model-list-css >>>.ivu-card-body {
        padding:0;
    }

    .check-up-button{
        height: 30px;
        margin-left:10px;
    }

    .detailRow1{
        border: 1px solid #E0E0EC;
        font-size:12px;
        font-family:Microsoft YaHei;
        font-weight:400;
        color:rgba(51,51,51,1);
        line-height:48px;
    }

    .detailRow2{
        background:rgba(248,248,249,1);
        border-right: 1px solid #E0E0EC;
        border-left: 1px solid #E0E0EC;
        font-size:12px;
        font-family:Microsoft YaHei;
        font-weight:400;
        color:rgba(51,51,51,1);
        line-height:48px;
    }

</style>